package com.cl.util;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PagerTag extends TagSupport{

	private static final long serialVersionUID = -7832642400955155086L;
	private Pager items;
	@Override
	public int doEndTag() throws JspException {
		try {
			if(items.getTotleCount()==0){//显示暂无数据
				String html = "<div class='row'>"
						+"<div class='col-sm-12'><center style='font-size:1.8rem'><i class='fa fa-exclamation-circle'></i>	对不起，暂无相关数据！</center></div>"
					+"</div>";
				JspWriter writer = pageContext.getOut();
				writer.print(html);
				return EVAL_PAGE;
			}
			int start = (items.getPage()-1)*items.getPageSize()+1;
			int end = items.getPage()*items.getPageSize() > items.getTotleCount() ? items.getTotleCount() : items.getPage()*items.getPageSize();
			String first = items.getPage() > 1 ? "" : "class='ui-disabled'";
			String prve = items.getPage() > 1 ? "" : "class='ui-disabled'";
			String next = items.getPages() > items.getPage() ? "" : "class='ui-disabled'";
			String last = items.getPages() > items.getPage() ? "" : "class='ui-disabled'";
			String firstClick = items.getPage() > 1 ? "onclick='firstPage()'" : "";
			String prveClick = items.getPage() > 1 ? "onclick='prvePage()'" : "";
			String nextClick = items.getPages() > items.getPage() ? "onclick='nextPage()'" : "";
			String lastClick = items.getPages() > items.getPage() ? "onclick='lastPage()'" : "";
			String selected20 = items.getPageSize() == 20 ? "selected" : "";
			String selected50 = items.getPageSize() == 50 ? "selected" : "";
			String selected100 = items.getPageSize() == 100 ? "selected" : "";
			JspWriter writer = pageContext.getOut();
			String html = "<div class='row'>"
							+"<div class='col-sm-6'>"
								+"<div class='dataTables_info'>显示 "+start+" 到 "+end+" 项，共 "+items.getTotleCount()+" 项，每页 <select name='pageSize' onchange='firstPage()'><option "+selected20+" value='20'>20</option><option "+selected50+" value='50'>50</option><option "+selected100+" value='100'>100</option></select> 项</div>"
							+"</div>"
							+"<div class='col-sm-6'>"
								+"<div class='dataTables_paginate paging_simple_numbers'>"
									+"<ul class='pagination'>"
										+"<li "+first+"><a "+firstClick+" style='border: none;' title='第一页' class='glyphicon glyphicon-step-backward'></a></li>"
										+"<li "+prve+"><a "+prveClick+" style='border: none;' title='上一页' class='glyphicon glyphicon-backward'></a></li>"
										+"<li><input id='currPage' type='hidden' name='page' value='"+items.getPage()+"'><a style='border: none;' title='当前页'>"+items.getPage()+"</a></li>"
										+"<li "+next+"><a "+nextClick+" style='border: none;' title='下一页' class='glyphicon glyphicon-forward'></a></li>"
										+"<li "+last+"><a "+lastClick+" style='border: none;' title='最后一页' class='glyphicon glyphicon-step-forward'></a></li>"
									+"</ul>"
								+"</div>"
							+"</div>"
						+"</div>";
			writer.print(html);
			String js = "<script type='text/javascript'>"
							+"function firstPage(){"
							+"$('#currPage').val(1);"
							+"$('#search_from').submit();"
							+"}"
							+"function prvePage(){"
							+"$('#currPage').val("+(items.getPage()-1)+");"
							+"$('#search_from').submit();"
							+"}"
							+"function nextPage(){"
							+"$('#currPage').val("+(items.getPage()+1)+");"
							+"$('#search_from').submit();"
							+"}"
							+"function lastPage(){"
							+"$('#currPage').val("+items.getPages()+");"
							+"$('#search_from').submit();"
							+"}"
						+"</script>";
			writer.print(js);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
	public Pager getItems() {
		return items;
	}
	public void setItems(Pager items) {
		this.items = items;
	}
}
